﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Security; 
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class updateEntry : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        if (Session["SELECTED_LANGUAGE"] != null)
        {
            string current_language = Session["SELECTED_LANGUAGE"].ToString();

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(current_language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(current_language);
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture;
        }

        base.InitializeCulture();
    }
    
    public int CompId=0;
        
    protected void Page_Load(object sender, EventArgs e)
    {
        Label2.Text = "Logged in as: " + Context.User.Identity.Name;

        logoutButton.Visible = true;
        
        CompId = Convert.ToInt32(Request.Params["CompId"]);

        if (CompId == 0)
        {
            Response.Redirect("Default.aspx");
        }

        cmpEntry updatedCmp = new cmpEntry();

        updatedCmp.cmpGet(CompId);

        cmpUser check_user=new cmpUser();
        
        if (!(check_user.get_id(Context.User.Identity.Name) == updatedCmp.cmpOwner))
        {
            Response.Redirect("Default.aspx");
        }
        
        if (!IsPostBack)
        {
            TextBox1.Text = updatedCmp.cmpName.Trim();
            TextBox2.Text = updatedCmp.cmpAddress.Trim();
            TextBox3.Text = updatedCmp.cmpEmail.Trim();
            TextBox4.Text = updatedCmp.cmpCountry.Trim();
            TextBox5.Text = updatedCmp.cmpCity.Trim();
            TextBox6.Text = updatedCmp.cmpPhone.Trim();
            TextBox7.Text = updatedCmp.cmpFax.Trim();
            TextBox8.Text = updatedCmp.cmpWebsite.Trim();

            Image1.ImageUrl = "logoView.aspx?CompId=" + CompId;                   
        }
    }

    public bool isImageChanged()
    {
        if (FileUpload1.FileName != "") 
        {
            return true;
        }
        else
        {
            return false; 
        }             
    } 

    protected void btnUpdate(object sender, EventArgs e)
    {
        cmpEntry newdataCmp = new cmpEntry();

        newdataCmp.cmpGet(CompId); 
        newdataCmp.app_path = Request.PhysicalApplicationPath;

        newdataCmp.cmpName=TextBox1.Text;
        newdataCmp.cmpAddress=TextBox2.Text;
        newdataCmp.cmpEmail=TextBox3.Text;
        newdataCmp.cmpPhone = TextBox6.Text;
        newdataCmp.cmpFax = TextBox7.Text;
        newdataCmp.cmpWebsite = TextBox8.Text;
        newdataCmp.cmpCountry = TextBox4.Text;
        newdataCmp.cmpCity = TextBox5.Text; 

        if (isImageChanged())
        {
            newdataCmp.cmpLogofile = FileUpload1.PostedFile;
        }
        else
        {
            newdataCmp.cmpLogofile = null; 
        }
        
        newdataCmp.cmpUpdate();

        Response.Redirect("viewDetails.aspx?CompId="+newdataCmp.cmpID);
    }
    
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("Default.aspx");
    }
    protected void btnCancel(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");                      
    }

    protected void Link_EN_Click(object sender, EventArgs e)
    {
        Session.Add("SELECTED_LANGUAGE", "en-US");
        Response.Redirect(Request.Url.AbsoluteUri);
    }
    protected void Link_RU_Click(object sender, EventArgs e)
    {
        Session.Add("SELECTED_LANGUAGE", "ru-RU");
        Response.Redirect(Request.Url.AbsoluteUri);
    }
}